#pragma  once
#include "Common/CharacterDefine.h"
#include "Scene.h"
#include "base/core/object_pool.h"
#include "base/core/singleton.h"

class Scene;
class Grid;
class Creature;
class Player;
class Monster;
class Npc;
class Drop;

class CRebot;
typedef std::vector<Point2<int> > vecPoint2;

enum
{
	LEFT_OF_GRID = 0,
	RIGHT_OF_GRID = 1,
};

enum
{
	TOP_OF_GRID = 0,
	BOTTOM_OF_GRID = 1,
};

#define MAX_LAYER 5  //搜索最大层数
class SceneMgr : public ManualSingleton<SceneMgr>, public IServerMessageHandler
{
	typedef std::unordered_map<uint32_t, Scene*> mapScenes;
	typedef std::unordered_map<uint32_t, ObjectPool<Scene>*> mapObjPool;
	typedef std::unordered_map <int, vector<Point2<int> > > mapLayer;
	typedef std::unordered_map <uint32_t, int64_t> DELMAP;
	typedef std::list <uint32_t> CONFIGMAPIDLST;

protected: 
	//每个地图Id对应的map映射内存池
	mapObjPool m_mapSceneObjPool;
	//此逻辑节点中当前场景对象
	mapScenes m_Scenes;
	//场景销毁缓存表
	DELMAP m_delCacheMap;
    //每层映射搜索位置信息(最大5层)
	mapLayer m_mapLayer;
	//当前场景实例Id
	uint32_t m_countId;
	//当前逻辑服跑的场景id集合
	CONFIGMAPIDLST m_mapIdsLst;

	//场景切换回复
	bool TransSceneRspCenter(const char *data, uint32_t len);
	//生物切换场景结果
	bool CreatureTransSceneResult(const char *data, uint32_t len);
	//生物切换场景成功
	bool CreatureTransSceneSuccess(const char *data, uint32_t len);
	//创建场景推送
	bool CreateSceneReq(const char *data, uint32_t len);

public:
	bool Init();
	bool UnInit();
	bool Update(uint64_t tick);
	virtual void onServerMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getServerHandlerDebugInfo(void){ return nullptr; };

	//创建静态场景
	bool InitRunSceneInLogic(CONFIGMAPIDLST& list);
	bool InitRunSceneUseMapConfig(CONFIGMAPIDLST& list);
	//创景场景
	Scene* CreateScene(uint32_t mapId,uint32_t nSceneID);
	//场景销毁
	bool DestoryScene(uint32_t scecenId);
	//获取场景
	Scene* GetScene(uint32_t sceneId);
	//回收场景
	bool RecyclScene(uint32_t sceneId);
	//获取层数对应格子计算数组
	const vecPoint2* GetLayerPoint(uint32_t nlayer);
	//是否关闭
	bool IsClosed(uint32_t sceneId);
	//
	SceneMgr();
	~SceneMgr();
public:
	Creature* GetCreature(CharIDType cid);
	Creature* GetCreature(uint32_t SceneId, CharIDType Cid);
	//玩家接口
	Player *GetPlayer(CharIDType cid);
	Player *GetPlayer(uint32_t sceneId,CharIDType cid);
	int32_t GetPlayerCount();
	//怪物接口
	Monster *GetMonster(CharIDType cid);
	Monster *GetMonster(uint32_t sceneId, CharIDType cid);
	//npc接口
	Npc *GetNpc(CharIDType cid);
	Npc *GetNpc(uint32_t sceneId, CharIDType cid);
	//掉落接口
	Drop *GetDrop(CharIDType cid);
	Drop *GetDrop(uint32_t sceneId, CharIDType cid);
	//机器人接口
	CRebot *GetRebot(CharIDType cid);	
	CRebot *GetRebot(uint32_t sceneId, CharIDType cid);
};

#define g_MakeSceneMgr() (SceneMgr::Instance())
#define g_GetSceneMgr()  (SceneMgr::GetInstance())
#define g_DelSceneMgr()  (SceneMgr::Destroy())
